const express = require('express');

const bodyParser = require('body-parser');

module.exports = () => {
  console.log('init express');

  const app = express();
  app.use(bodyParser.json());
  app.use(express.static('../public'));

  
  const NewRouter = require('../app/routers/news.server.routers.js');
  NewRouter(app);

  app.use((req, res, next) => {
    res.status(404);
    try {
      return res.json('not found');
    } catch (error) {
      console.error('404 not found');
    }

  });

  app.use((err, req, res, next) => {
    if (!err) {
      return next();
    }
    res.status(500);
    try {
      return res.json(err.message || 'server error');
    } catch (error) {
      console.error('500 set header after sent');
    }
  })

  return app;
}